package com.wanbatv.wanba.ahitv.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.wanbatv.wanba.ahitv.Fittimeutil.Shouye_Image;
import com.wanbatv.wanba.ahitv.R;
import com.wanbatv.wanba.ahitv.util.DataReport;
import com.wanbatv.wanba.ahitv.util.Util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class FittimeActivity extends Activity {
    private Shouye_Image fit_shouye_iv1;
    private Shouye_Image fit_shouye_iv2;
    private Shouye_Image fit_shouye_iv3;
    private Shouye_Image fit_shouye_iv4;
    private TextView fit_main_time;
    private static final int msgKey1 = 1;
    public static int WHICH_NUM=0;
    private ImageView fit_daka_iv;
    private ImageView fit_time_erweima;
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fittime);
        fit_shouye_iv1=(Shouye_Image)findViewById(R.id.fit_shouye_iv1);
        fit_shouye_iv2=(Shouye_Image)findViewById(R.id.fit_shouye_iv2);
        fit_shouye_iv3=(Shouye_Image)findViewById(R.id.fit_shouye_iv3);
        fit_shouye_iv4=(Shouye_Image)findViewById(R.id.fit_shouye_iv4);
        ImageView a1=(ImageView)fit_shouye_iv2.findViewById(R.id.shouye_image_1);
        ImageView a2=(ImageView)fit_shouye_iv3.findViewById(R.id.shouye_image_1);
        ImageView a3=(ImageView)fit_shouye_iv4.findViewById(R.id.shouye_image_1);
        tv1=(TextView)fit_shouye_iv1.findViewById(R.id.shouye_image_tv);
        tv2=(TextView)fit_shouye_iv2.findViewById(R.id.shouye_image_tv);
        tv3=(TextView)fit_shouye_iv3.findViewById(R.id.shouye_image_tv);
        tv4=(TextView)fit_shouye_iv4.findViewById(R.id.shouye_image_tv);
        a1.setImageResource(R.drawable.fittime_bailingnan);
        a2.setImageResource(R.drawable.fittime_wuyang);
        a3.setImageResource(R.drawable.fittime_nengliang);
        RequestQueue rq1 = Volley.newRequestQueue(this.getApplicationContext());
        JsonObjectRequest request1 = new JsonObjectRequest("http://61.191.46.214:8087/fittime/usercount/categorys", null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            JSONObject obj=jsonObject.getJSONObject("data");
                            JSONArray arr=obj.getJSONArray("list");
                            for(int i=0;i<arr.length();i++){
                                JSONObject d=arr.getJSONObject(i);
                                if(d.getString("category").equals("001")){
                                    tv1.setText("当前人数"+d.getString("userCount")+"人");
                                }else if(d.getString("category").equals("002")){
                                    tv2.setText("当前人数"+d.getString("userCount")+"人");
                                }else if(d.getString("category").equals("003")){
                                    tv3.setText("当前人数"+d.getString("userCount")+"人");
                                }else if(d.getString("category").equals("004")){
                                    tv4.setText("当前人数"+d.getString("userCount")+"人");
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }

        );
        rq1.add(request1);

        fit_time_erweima=(ImageView)findViewById(R.id.fit_time_erweima);

        fit_daka_iv=(ImageView)findViewById(R.id.fit_daka_iv);
        fit_daka_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it=new Intent();
                it.setClass(FittimeActivity.this,FittimeFengxiangActivity.class);
                startActivity(it);
            }
        });

        ImageView erji_iv1=(ImageView)fit_shouye_iv1.findViewById(R.id.shouye_image_gb);
        ImageView erji_iv2=(ImageView)fit_shouye_iv2.findViewById(R.id.shouye_image_gb);
        ImageView erji_iv3=(ImageView)fit_shouye_iv3.findViewById(R.id.shouye_image_gb);
        ImageView erji_iv4=(ImageView)fit_shouye_iv4.findViewById(R.id.shouye_image_gb);
        erji_iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it=new Intent();
                it.setClass(FittimeActivity.this,FittimeErjiActivity.class);
                startActivity(it);
                WHICH_NUM=1;
            }
        });
        erji_iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it=new Intent();
                it.setClass(FittimeActivity.this,FittimeErjiActivity.class);
                startActivity(it);
                WHICH_NUM=2;
            }
        });
        erji_iv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it=new Intent();
                it.setClass(FittimeActivity.this,FittimeErjiActivity.class);
                startActivity(it);
                WHICH_NUM=3;
            }
        });
        erji_iv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it=new Intent();
                it.setClass(FittimeActivity.this,FittimeErjiActivity.class);
                startActivity(it);
                WHICH_NUM=4;
            }
        });

        fit_main_time=(TextView)findViewById(R.id.fit_main_time);
        new TimeThread().start();

        RequestQueue rq = Volley.newRequestQueue(this.getApplicationContext());
        JsonObjectRequest request = new JsonObjectRequest("http://121.201.14.248/wanba/photo_ajax_0907.jsp?userId="+ Util.getDeviceId(this), null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            JSONObject obj=jsonObject.getJSONObject("qrcode");
                            String iv_url="http://121.201.14.248"+obj.getString("url");

                            RequestQueue mQueue = Volley.newRequestQueue(FittimeActivity.this);
                            ImageRequest imageRequest = new ImageRequest(
                                    iv_url,
                                    new Response.Listener<Bitmap>() {
                                        @Override
                                        public void onResponse(Bitmap response) {
                                            fit_time_erweima.setImageBitmap(response);
                                        }
                                    }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                }
                            });
                            mQueue.add(imageRequest);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }

        );
        rq.add(request);
    }
    public class TimeThread extends Thread {
        @Override
        public void run () {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = msgKey1;
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while(true);
        }
    }
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage (Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case msgKey1:
                    long sysTime = System.currentTimeMillis();
                    CharSequence sysTimeStr = DateFormat.format("hh:mm aa", sysTime);
                    fit_main_time.setText(sysTimeStr);
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_fittime, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
        DataReport.PageRetreat(FittimeActivity.this, "1100", "200024");
    }

    @Override
    protected void onStop() {
        super.onStop();
        DataReport.PageRetreat(FittimeActivity.this, "2100", "200024");
    }
}
